/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yazdankhah.utils;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author ahmad
 */
public class UnicodeUtil {
  
  //private static Logger logger = Logger.getLogger(UnicodeUtil.class);

  /**
   * Makes a string containing the information about 
   * the give string as a sequence of Unicode characters.
   *
   * @param str - the give string
   */
  private static String makeUnicodeStringInfo(String str) {

    char[] cArr = str.toCharArray();
    //byte[] cArr = str.getBytes(Charset.forName("utf-8"));
    //byte[] cArr = str.getBytes(StandardCharsets.UTF_8);

    StringBuilder sb = new StringBuilder("String = [");

    for (int i = 0; i < str.length(); i++) {
      sb.append(String.format("\\U%04x", Character.codePointAt(cArr, i)));
    } //for
    
    sb.append("]\n\n");

    for (int i = 0; i < str.length(); i++) {
      sb.append(
      String.format("Char %3d: char = [%c] \t\t Unicode = [U+%04x] \t Number of code Unit = [%d]%n",
                      i, cArr[i], Character.codePointAt(cArr, i), Character.charCount(i)));
    } //for
    
    return sb.toString();

  } // method


  /**
   * Shows the give string as a sequence of Unicode characters.
   *
   * @param str - the give string
   */
  public static void showUnicodeStringInfo(String str, Logger logger) {
    
    String tempStr = makeUnicodeStringInfo(str);

    //System.out.println(tempStr);
    logger.debug(tempStr);

  } // method


  private static void testshowUnicodeStringInfo() {

    String str = "\u00C6\u00D8\u00C5";

    //showUnicodeStringInfo(str);

  } // method


  /**
   * Writes the give string as a sequence of Unicode characters in a given file.
   *
   * @param str - the give string
   * @param outFN - the output file name
   */
  public static void writeUnicodeStringInfo(String str, String outFN) {

    String tempStr = makeUnicodeStringInfo(str);
    
    FileUtil.stringToFile(tempStr, outFN);

  } // method


  private static void testwriteUnicodeStringInfo() {

    String str = "\u00C6\u00D8\u00C5";
    String outFN  = "quran\\t1_Out.txt";

    writeUnicodeStringInfo(str, outFN);

  } // method
  
  
  /**
   * Shows the content of an array containing UTF-8 Strings.
   *
   * @param unicodeStrArr - the array
   * @param msg show this message for clarification.
   * @throws java.io.UnsupportedEncodingException
   */
  public static void showUnicodeStringArray(String[] unicodeStrArr, String msg) 
          throws UnsupportedEncodingException {
    
    System.setOut(new PrintStream(System.out, true, "utf-8"));

    System.out.println("**********" + msg + "**********\n");

    for (int i = 0; i < unicodeStrArr.length; i++) {
      System.out.printf("Element #%d = [%s]%n", i, unicodeStrArr[i]);
    }

    System.out.println("\n///////////" + msg + "////////////");
      
  } //method


  /**
   * Shows the content of a given double array containing Unicode strings.
   *
   * @param arr - the given double array
   * @param strMaxLength the max length of the strings
   * @param msg the message to clarify the array
   * @throws java.io.UnsupportedEncodingException
   */
  public static void showUnicodeStringDoubleArray(String[][] arr, int strMaxLength, String msg) 
          throws UnsupportedEncodingException {
    
    System.setOut(new PrintStream(System.out, true, "utf-8"));

    System.out.println("**********" + msg + "**********");

    for (int i = 0; i < arr.length; i++) {
      for (int j = 0; j < arr[0].length; j++) {
        System.out.format("%" + strMaxLength + "s", arr[i][j]);
      }

      System.out.println("/inner-loop!");

    } //outer-for

    System.out.println("///////////" + msg + "////////////");

  } //method


  /**
   * Shows the content of a given Unicode list.
   *
   * @param <E> the type parameter for the List.
   * @param arr - the given list
   * @param msg - the message to clarify the output
   * @throws java.io.UnsupportedEncodingException
   */
  public static <E> void showUnicodeArrayList(List<E> arr, String msg) 
          throws UnsupportedEncodingException {
    
    System.setOut(new PrintStream(System.out, true, "utf-8"));
    
    System.out.println("**********" + msg + "**********");

    for (int i = 0; i < arr.size(); i++) {
      System.out.format(
              "\nElement #%d = [%s]%n", i, arr.get(i).toString());
    }

    System.out.println("///////////" + msg + "////////////");

  } //method


  


//........................ M A I N   M E T H O D ............................//
  /**
   * This main method is just for testing this class.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {

    testwriteUnicodeStringInfo();

  } // main method.

} // class

